/**
 * 控制器
 */
vshopApp.controller('loginController', ['$rootScope', '$scope', '$http','$location', function($rootScope, $scope,$http,$location) {
	$scope.$on('$viewContentLoaded', function() {
		console.log('页面加载完成');
	});
	$scope.login = function(){
		user = $scope.user;
		$http.post('auth',user).success(function(data){
			localStorage.setItem("token",data.token);
			$location.path("/");
		}).error(function(data, status, headers, config){
        });
	}
}]);

vshopApp.controller('productController', ['$rootScope', '$scope', '$http','$location', function($rootScope, $scope,$http,$location) {
	$scope.$on('$viewContentLoaded', function() {
		var selectedCustomer = JSON.parse(localStorage.getItem("selectedCusomer"));
		$scope.selectedCustomer = selectedCustomer;
		if(selectedCustomer==null){
			//跳转到选择客户页面
			$location.path("/selectCustomer");
		}
		
		$http.get('product/search/'+selectedCustomer.id).success(function(data){
			$scope.page=data;
		});
	});
	
	$scope.search = function(){
		keywords = $scope.keywords;
		$http.get('product/search',{
			params:{keywords:keywords}
		}).success(function(data){
			$scope.page=data;
			
		});
	};
	
	$scope.addToCart = function(product,price,qty){
		if(qty!=null){
			var selectedCustomer = JSON.parse(localStorage.getItem("selectedCusomer"));
			var data = {'customer':{'id':selectedCustomer.id},'product':{'id':product.id,'price':product.price},'qty':qty,'price':price};
			$http.post('/shoppingcart/add',data).success(function(data){
				console.log('添加购物车成功');
				product.cartQty = qty;
			});
		}
	}

}]);
vshopApp.controller('orderController', ['$rootScope', '$scope', '$http','$location', function($rootScope, $scope,$http,$location) {
	$scope.$on('$viewContentLoaded', function() {
		$http.get('order/search').success(function(data){
			$scope.page=data;
		});
	});
	
	$scope.search = function(){
		keywords = $scope.keywords;
		$http.get('order/search',{
			params:{keywords:keywords}
		}).success(function(data){
			$scope.page=data;
			
		});
	};

}]);

vshopApp.controller('customerController', ['$rootScope', '$scope', '$http','$location', function($rootScope, $scope,$http,$location) {
	$scope.$on('$viewContentLoaded', function() {
		$http.get('customer/search').success(function(data){
			$scope.page=data;
		});
	});
	
	$scope.search = function(){
		keywords = $scope.keywords;
		$http.get('customer/search',{
			params:{keywords:keywords}
		}).success(function(data){
			$scope.page=data;
		});
	};
	$scope.selectCustomer = function(customer){
		localStorage.setItem("selectedCusomer",JSON.stringify(customer));
		$location.path('/');
	};

}]);


vshopApp.controller('shoppingcartController', ['$rootScope', '$scope', '$http','$location', function($rootScope, $scope,$http,$location) {
	$scope.$on('$viewContentLoaded', function() {
		$http.get('shoppingcart').success(function(data){
			$scope.carts=data;
			var total =0 ;
			for(x in data){
				total+=data[x].totalAmount;
			}
			$scope.totalAmount= total;
		});
	});
	$scope.makeOrder = function(){
		$http.post('shoppingcart/makeOrder').success(function(data){
			$location.path("/order");
		});
	}
	$scope.removeCart = function(cart){
		$http.post('shoppingcart/clear',cart).success(function(data){
			$http.get('shoppingcart').success(function(data){
				$scope.carts=data;
				var total =0 ;
				for(x in data){
					total+=data[x].totalAmount;
				}
				$scope.totalAmount= total;
			});
		});
	}
	$scope.updateOrderItem = function(orderItem){
		var selectedCustomer = JSON.parse(localStorage.getItem("selectedCusomer"));
		orderItem.customer = selectedCustomer;
		$http.post('/shoppingcart/add',orderItem).success(function(data){
			console.log('修改购物车成功');
			$http.get('shoppingcart').success(function(data){
				$scope.carts=data;
				var total =0 ;
				for(x in data){
					total+=data[x].totalAmount;
				}
				$scope.totalAmount= total;
			});
		});
		
	}
	
}]);

vshopApp.controller('serviceController', ['$rootScope', '$scope', '$http', function($rootScope, $scope,$http) {
	$scope.$on('$viewContentLoaded', function() {
	});
}]);

vshopApp.controller('usercenterController', ['$rootScope', '$scope', '$http', function($rootScope, $scope,$http) {
	$scope.$on('$viewContentLoaded', function() {
	});
}]);







